﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebsiteBanThucAn.Models;
using System.Data;
using WebsiteBanThucAn.DAL;

namespace DAL
{
    public class KhachHangDAL
    {
        private static void GanDoiTuong(KhachHangModel khm, DataRow dr)
        {// Gán từng thuộc tính cho đối tượng
            khm.KhachhangID = int.Parse(dr[KhachHangModel.khachhangid].ToString());
            khm.Hoten = dr[KhachHangModel.hoten].ToString();
            khm.Diachi = dr[KhachHangModel.diachi].ToString();
            khm.Dienthoai = dr[KhachHangModel.dienthoai].ToString();
            khm.Tendangnhap = dr[KhachHangModel.tendangnhap].ToString();
            khm.Matkhau = dr[KhachHangModel.matkhau].ToString();
            khm.Ngaysinh = DateTime.Parse(dr[KhachHangModel.ngaysinh].ToString());
            khm.Gioitinh = bool.Parse(dr[KhachHangModel.gioitinh].ToString());
            khm.Email = dr[KhachHangModel.email].ToString();
        }

        public static List<KhachHangModel> LayDSKhachHangTatCa()
        {// Lấy danh sách tất cả khách hàng
            List<KhachHangModel> list = new List<KhachHangModel>();
            try
            {
                string sql = string.Format("select * from {0}", KhachHangModel.khachhang);
                DataTable dt = SqlDataProvider.DocBang(sql);
                foreach (DataRow dr in dt.Rows)
                {
                    KhachHangModel khm = new KhachHangModel();
                    GanDoiTuong(khm, dr);
                    list.Add(khm);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }

        public static KhachHangModel LayThongTinKhachHangTheoID(int id)
        {// Lấy thông tin khách hàng theo ID
            KhachHangModel khm = new KhachHangModel();
            try
            {
                string sql = string.Format("select * from {0} where {1}='{2}'", KhachHangModel.khachhang, KhachHangModel.khachhangid, id);
                DataTable dt = SqlDataProvider.DocBang(sql);
                foreach (DataRow dr in dt.Rows)
                {
                    GanDoiTuong(khm, dr);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return khm;
        }

        public static bool ThemKhachHang(KhachHangModel khm)
        {
            bool result = false;
            try
            {
                string sql = string.Format("insert into {0} values ", KhachHangModel.khachhang);
                sql += string.Format("(N'{0}',N'{1}','{2}','{3}','{4}',convert(date,'{5}',101),'{6}','{7}')",
                    khm.Hoten, khm.Diachi, khm.Dienthoai, khm.Tendangnhap, khm.Matkhau, khm.Ngaysinh, khm.Gioitinh, khm.Email);
                int n = SqlDataProvider.ThucHienLenh(sql);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static bool XoaKhachHang(int id)
        {
            bool result = false;
            try
            {
                string sql = string.Format("delete {0} where {1}='{2}'", KhachHangModel.khachhang, KhachHangModel.khachhangid, id);
                int n = SqlDataProvider.ThucHienLenh(sql);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static bool SuaKhachHang(KhachHangModel khm)
        {
            bool result = false;
            try
            {
                string sql = string.Format("update {0} set ", KhachHangModel.khachhang);
                sql += string.Format("{0}=N'{1}',{2}=N'{3}',{4}='{5}',{6}='{7}',{8}='{9}',{10}=convert(date,'{11}',101),{12}='{13}',{14}='{15}'",
                    KhachHangModel.hoten,
                    khm.Hoten,
                    KhachHangModel.diachi,
                    khm.Diachi,
                    KhachHangModel.dienthoai,
                    khm.Dienthoai,
                    KhachHangModel.tendangnhap,
                    khm.Tendangnhap,
                    KhachHangModel.matkhau,
                    khm.Matkhau,
                    KhachHangModel.ngaysinh,
                    khm.Ngaysinh,
                    KhachHangModel.gioitinh,
                    khm.Gioitinh,
                    KhachHangModel.email,
                    khm.Email);
                sql += string.Format(" where {0}='{1}'", KhachHangModel.khachhangid, khm.KhachhangID);
                int n = SqlDataProvider.ThucHienLenh(sql);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
    }
}
